<?php

class loginForm_ extends sfForm {

    public function configure() {
        // i18n
        $_i18 = sfContext::getInstance()->getI18N();
        /*
         * username - password widget
         */
        $this->setWidget('username', new sfWidgetFormInputText(array('label' => 'username ')));
        $this->setWidget('password', new sfWidgetFormInputPassword());
        // captcha field if option is true
        if ($this->getOption('captcha') == true) {
            $this->setWidget('captcha', new sfWidgetCaptchaGD());
            $this->setValidator('captcha', new sfCaptchaGDValidator(array('length' => 6), array('invalid' => "Incorrect.", 'required' => 'Required.')));
        }
        // username validator email or username - required
        $this->setValidator('username', new sfValidatorOr(array(
                    new sfValidatorRegex(array('pattern' => '/^(?=.*[a-zA-Z\d]).{4,15}$/', 'min_length' => 4, 'max_length' => 15)),
                    new sfValidatorRegex(array('pattern' => '/^[a-z][a-z0-9_.-]*@[a-z0-9.-]+[.][a-z]{2,}$/ii', 'min_length' => 5, 'max_length' => 255, 'required' => true))),
                        array('required' => true), array(
                    'invalid' => $_i18->__('Please enter a valid username or email.'),
                    'required' => $_i18->__('Required field, Please type your username or email.')
                )));
        // password 6-15 required
        $this->setValidator('password', new sfValidatorString(array('max_length' => 15, 'required' => true, 'min_length' => 6), array(
                    'invalid' => $_i18->__('Please enter a valid password.'),
                    'required' => $_i18->__("Required field."),
                    'max_length' => $_i18->__('Too long.'),
                    'min_length' => $_i18->__('Too short.')
                )));

        // username help
        $this->getWidgetSchema()->setHelp('username', $_i18->__('Please type your username or email.'));
        // set name format
        $this->widgetSchema->setNameFormat('login[%s]');
    }

}